package com.yt.system.dao;

import com.yt.system.domain.Task;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TaskMapper {
    int deleteByPrimaryKey(Integer tid);

    /**
     * 用户添加任务
     * @param record
     * @return
     */
    int insert(Task record);

    int insertSelective(Task record);

    Task selectByPrimaryKey(Integer tid);

    int updateByPrimaryKeySelective(Task record);

    int updateByPrimaryKey(Task record);

    /**
     * 用户提交任务，修改任务的状态
     * @param tid
     * @param tStatus
     */
    void updateTaskByTid(@Param("tid") Integer tid, @Param("tStatus") Integer tStatus);

    /**
     * 查询用户个人当天的任务
     * @param aid
     * @param tStartTime
     * @return
     */
    @Select("select user.uname,account.aacount,todo.tid,todo.aid,todo.tcontent,todo.tstarttime,todo.tendtime,todo.tstatus,todo.treason " +
            "from (select * from task where aid = #{aid} and tstarttime like '${tStartTime}%') as todo left join account " +
            "on account.aid = todo.aid left join user on user.uid = todo.aid")
    List<Task> findUserTaskByAid(@Param("aid") Integer aid,@Param("tStartTime") String tStartTime);

    /**
     * 查询当前用户所在组别的其他组员的当天任务
     * @param aid
     * @param tStartTime
     * @return
     */
    @Select("select user.uname,account.aacount,todo.tid,todo.aid,todo.tcontent,todo.tstarttime,todo.tendtime,todo.tstatus,todo.treason from " +
            "(select * from task where aid in (select uid from group_message where gid =(select gid from group_message where " +
            "uid = #{aid} and status = 0) and status = 0 and tstarttime like'${tStartTime}%') and aid != #{aid}) as todo left join " +
            "account on account.aid = todo.aid left join user on user.uid = todo.aid")
    List<Task> findTeamOtherTask(@Param("aid") Integer aid,@Param("tStartTime")String tStartTime);

    /**
     * 根据tid查询任务
     * @param tid
     * @return
     */
    @Select("select * from task where tid = #{tid}")
    Task findTaskById(Integer tid);

    @Update("update task set tstatus = 0,treason = #{treason} where tid = #{tid}")
    void updateTask(Task task);
}